import routes from "../../routers"; // 引入路由配置
import store from "../../vuex/store";
import { mapState } from "vuex";
import { savaToLocal, loadFromlLocal } from "./storage.js"; // 公共方法：本地缓存
import FastClick from "fastclick";
import { formatDate } from "./date.js";

//给window注入全局方法，在组件里面可以直接用window.getCookie()来调用
window.getCookie = function getCookie(name) {
  var arr,
    reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

  if ((arr = document.cookie.match(reg))) return unescape(arr[2]);
  else return null;
};

window.setCookie = function setCookie(name, value) {
  var Days = 30;
  var exp = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie =
    name + "=" + escape(value) + ";expires=" + exp.toGMTString();
};

window.setDocumentTitle = title => {
  document.title = title;
  if (/ip(hone|od|ad)/i.test(navigator.userAgent)) {
    var i = document.createElement("iframe");
    i.src = "/favicon.ico";
    i.style.display = "none";
    i.onload = function() {
      setTimeout(function() {
        i.remove();
      }, 9);
    };
    document.body.appendChild(i);
  }
};

//消除移动端点击300ms延迟
var str = navigator.userAgent.toLowerCase();
var ver = str.match(/cpu iphone os (.*?) like mac os/);
if (!ver) {
  //非IOS系统
  // 引入fastclick文件
  FastClick.attach(document.body);
  store.commit("UPDATA_SYSTEM", "Android");
} else {
  console.log("你当前的Ios系统版本为：" + ver[1].replace(/_/g, "."));
  if (parseInt(ver[1]) >= 11) {
    //不必引入fastclick文件
  } else {
    // 引入fastclick文件
    FastClick.attach(document.body);
  }
  store.commit("UPDATA_SYSTEM", "ios");
}

export default {
  install(Vue, options) {
    Vue.prototype.getInitData = function(backfun = null) {
      //给自定义方法起个名
      Vue.prototype.$http
        .all([
          Vue.prototype.$http.post(store.state.path + `store/User/userinfo`, {
            type: "userinfo",
            uid: store.state.userid
          }),
          Vue.prototype.$http.post(store.state.path + `store/Store/storeinfo`, {
            storeid: store.state.storeid
          })
        ])
        .then(
          Vue.prototype.$http.spread(function(userResp, storeResp) {
            // 上面两个请求都完成后，才执行这个回调方法
            let userData = userResp.data.result.uinfo;
            if (userData.phone == null) userData.phone = "";
            store.commit("UPDATE_PHONENO", userData.phone);
            store.commit("UPDATE_ROLE", userData.role);
            // store.commit("UPDATE_ROLE", '2');
            store.commit("UPDATE_ICON", userData.headimgurl);
            store.commit("UPDATE_USERNAME", userData.username);
            store.commit("UPDATE_BALANCE", parseFloat(userData.accounts));
            store.commit("UPDATE_REALNAME", userData.fullname);
            store.commit("UPDATE_SOURCE", userData.source);
            store.commit(
              "UPDATE_BIRTHDAY",
              formatDate(new Date(parseInt(userData.birthday) * 1000), "yyyy-MM-dd")
            );
            if (userData.sex.toString() == "1") store.commit("UPDATE_SEX", "男");
            else store.commit("UPDATE_SEX", "女");
            let storeData=storeResp.data.result;
            store.commit("UPDATE_STORE_INFO", storeData);

            if(userData.role.toString()=='1')  store.commit("UPDATE_SERVICE_TEL", storeData.phone);
            else store.commit("UPDATE_SERVICE_TEL", '13916403296');

            let giftList=userResp.data.result.award;
            store.commit("UPDATA_GIFT_LIST", giftList);
            
            if (backfun != null && backfun != undefined) {
              backfun();
            }
          })
        );
    };
  }
};
